package com.changpay.zywy.ui.activity.count

import android.support.v7.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.ActionMonthAdapter
import com.changpay.zywy.app.App
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.bean.MonthActiontionBean
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.ui.dialog.SelectMonthDialog
import com.changpay.zywy.utils.DateUtil
import kale.ui.view.dialog.EasyDialog
import kotlinx.android.synthetic.main.activity_action_month_count.*
import kotlinx.android.synthetic.main.title_layout.*


class CountActionMonthActivity : BaseActivity(), IView {




    override fun attachLayoutRes(): Int {
        return R.layout.activity_action_month_count
    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }


    override fun initData() {
    }

    override fun initView() {
        tv_title.text = "月激活量"
        iv_back.setOnClickListener { finish() }
        initRecyclerView()

        tv_time_search.setOnClickListener { view ->
            //                DialogUtils.showDatePickerDialog(context, onDateSetListener, Calendar.getInstance());
            val builder = SelectMonthDialog.Builder(this)
            builder.setOnDialogClickListener { v, month ->
                tv_action_month_text.text = month + "激活量"
                loadDatas(month)
            }
            val build = builder.build<EasyDialog>()
            build.isCancelable = true
            build.show(supportFragmentManager)
        }
    }

    override fun start() {
        val time = DateUtil.getYearMonth(System.currentTimeMillis())
        loadDatas(time)
    }

    fun loadDatas(month: String) {
        val param = hashMapOf<String, String>()
        param["key"] = token
        param["month"] = month
        param["gc_id"] = intent.getStringExtra("gc_id")
        RetrofitHelper.service.get_month_activation(param).sss(this)
        {
            val datas = it.result.activation
            var total = 0
            for (i in datas.indices) {
                datas[i].type = i % 4
                total += datas[i].num
            }
            for (i in datas.indices) {
                datas[i].total = total
            }
            this.datas.clear()
            this.datas.addAll(datas)
            mAdapter.notifyDataSetChanged()
        }
    }


    /**
     * datas
     */
    private val datas = mutableListOf<MonthActiontionBean.ActivationBean>()
    /**
     * Adapter
     */
    private val mAdapter: ActionMonthAdapter by lazy {
        ActionMonthAdapter(App.context, datas)
    }


    /**
     * LinearLayoutManager
     */
    private val mLayoutManager: GridLayoutManager by lazy {
        GridLayoutManager(App.context, 2)
    }


    /**
     * ItemClickListener
     */
    private val ItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->

    }


    /**
     * ItemChildClickListener
     */
    private val onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener { _, view, position ->


    }

    private fun initRecyclerView() {
        recyclerView_month.run {
            layoutManager = mLayoutManager
            adapter = mAdapter
//            itemAnimator = DefaultItemAnimator()
//            recyclerViewItemDecoration.let { addItemDecoration(it) }
        }
        mAdapter.run {
            bindToRecyclerView(recyclerView_month)
            onItemChildClickListener = this@CountActionMonthActivity.onItemChildClickListener
//            setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
            onItemClickListener = this@CountActionMonthActivity.ItemClickListener
        }
    }

}
